home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBOPS.C < prev    next >
Text File  |  1990-06-21  |  5KB  |  223 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbops.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <stdlib.h>*/
  10. /*#include <string.h>*/
  11.  
  12. /* non-ansi headers */
  13. #include <bool.h>
  14.  
  15. /* library headers */
  16. #include <blkio.h>
  17. #include <btree.h>
  18.  
  19. /* local headers */
  20. #include "cbase_.h"
  21.  
  22. /*man---------------------------------------------------------------------------
  23. NAME
  24.      cb_alloc - allocate memory for cbase
  25.  
  26. SYNOPSIS
  27.      #include "cbase_.h"
  28.  
  29.      int cb_alloc(cbp);
  30.      cbase_t *cbp;
  31.  
  32. DESCRIPTION
  33.      The cb_alloc function allocates the memory needed by cbp.  The
  34.      memory is is initialized to all zeros.
  35.  
  36.      cb_alloc will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       cbp is not a valid cbase pointer.
  39.      [ENOMEM]       Not enough memory is available for
  40.                     allocation by the calling process.
  41.      [CBENOPEN]     cbp is not open.
  42.  
  43. SEE ALSO
  44.      cb_free.
  45.  
  46. DIAGNOSTICS
  47.      Upon successful completion, a value of 0 is returned.  Otherwise,
  48.      a value of -1 is returned, and errno set to indicate the error.
  49.  
  50. ------------------------------------------------------------------------------*/
  51. int cb_alloc(cbp)
  52. cbase_t *cbp;
  53. {
  54. #ifdef DEBUG
  55.     /* validate arguments */
  56.     if (!cb_valid(cbp)) {
  57.         CBEPRINT;
  58.         errno = EINVAL;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not open */
  63.     if (!(cbp->flags & CBOPEN)) {
  64.         CBEPRINT;
  65.         errno = CBENOPEN;
  66.         return -1;
  67.     }
  68.  
  69.     /* check for memory leak */
  70.     if (cbp->fldv != NULL || cbp->btpv != NULL) {
  71.         CBEPRINT;
  72.         errno = CBEPANIC;
  73.         return -1;
  74.     }
  75. #endif
  76.     /* field definition array */
  77.     cbp->fldv = (cbfield_t *)calloc((size_t)cbp->fldc, sizeof(*cbp->fldv));
  78.     if (cbp->fldv == NULL) {
  79.         CBEPRINT;
  80.         errno = ENOMEM;
  81.         return -1;
  82.     }
  83.  
  84.     /* btree array */
  85.     cbp->btpv = (btree_t **)calloc((size_t)cbp->fldc, sizeof(*cbp->btpv));
  86.     if (cbp->btpv == NULL) {
  87.         CBEPRINT;
  88.         free(cbp->fldv);
  89.         errno = ENOMEM;
  90.         return -1;
  91.     }
  92.  
  93.     errno = 0;
  94.     return 0;
  95. }
  96.  
  97. /*man---------------------------------------------------------------------------
  98. NAME
  99.      cb_free - free memory allocated for cbase
  100.  
  101. SYNOPSIS
  102.      #include "cbase_.h"
  103.  
  104.      void cb_free(cbp)
  105.      cbase_t *cbp;
  106.  
  107. DESCRIPTION
  108.      The cb_free function frees all memory allocated for cbase cbp.
  109.      If cbp is not a valid cbase, no action is taken.
  110.  
  111. SEE ALSO
  112.      cb_alloc.
  113.  
  114. ------------------------------------------------------------------------------*/
  115. void cb_free(cbp)
  116. cbase_t *cbp;
  117. {
  118. #ifdef DEBUG
  119.     /* validate arguments */
  120.     if (!cb_valid(cbp)) {
  121.         CBEPRINT;
  122.         return;
  123.     }
  124. #endif
  125.     /* free memory */
  126.     if (cbp->fldv != NULL) {
  127.         free(cbp->fldv);
  128.         cbp->fldv = NULL;
  129.     }
  130.     if (cbp->btpv != NULL) {
  131.         free(cbp->btpv);
  132.         cbp->btpv = NULL;
  133.     }
  134.  
  135.     return;
  136. }
  137.  
  138. /*man---------------------------------------------------------------------------
  139. NAME
  140.      cb_fvalid - validate field definition list
  141.  
  142. SYNOPSIS
  143.      #include "cbase_.h"
  144.  
  145.      bool cb_fvalid(recsize, fldc, fldv)
  146.      size_t recsize;
  147.      int fldc;
  148.      const cbfield_t fldv[];
  149.  
  150. DESCRIPTION
  151.      The cb_fvalid function determines if fldc and fldv constitute a
  152.      valid field definition list for a cbase with records of size
  153.      recsize.  If valid, then TRUE is returned.  If not, then FALSE is
  154.      returned.
  155.  
  156. ------------------------------------------------------------------------------*/
  157. bool cb_fvalid(recsize, fldc, fldv)
  158. size_t recsize;
  159. int fldc;
  160. const cbfield_t fldv[];
  161. {
  162.     size_t end = 0;
  163.     int i = 0;
  164.  
  165.     if (recsize < sizeof(bpos_t) || fldc < 1 || fldv == NULL) {
  166.         return FALSE;
  167.     }
  168.  
  169.     for (i = 0; i < fldc; ++i) {
  170.         if (fldv[i].offset < end) {
  171.             return FALSE;
  172.         }
  173.         if (fldv[i].len < 1) {
  174.             return FALSE;
  175.         }
  176.         end = fldv[i].offset + fldv[i].len;
  177.         if (end > recsize) {
  178.             return FALSE;
  179.         }
  180.         if (fldv[i].type < 0 || fldv[i].type >= CBTYPECNT) {
  181.             return FALSE;
  182.         }
  183.         if (fldv[i].flags & ~CB_FFLAGS) {
  184.             return FALSE;
  185.         }
  186.         if (fldv[i].flags & CB_FKEY) {
  187.             if (fldv[i].filename[0] == NUL) {
  188.                 return FALSE;
  189.             }
  190.         }
  191.     }
  192.  
  193.     return TRUE;
  194. }
  195.  
  196. /*man---------------------------------------------------------------------------
  197. NAME
  198.      cb_valid - validate cbase pointer
  199.  
  200. SYNOPSIS
  201.      #include "cbase_.h"
  202.  
  203.      bool cb_valid(cbp)
  204.      cbase_t *cbp;
  205.  
  206. DESCRIPTION
  207.      The cb_valid function determines if cbp is a valid cbase pointer.
  208.      If valid, then TRUE is returned.  If not, then FALSE is returned.
  209.  
  210. ------------------------------------------------------------------------------*/
  211. bool cb_valid(cbp)
  212. cbase_t *cbp;
  213. {
  214.     if (cbp < cbb || cbp > (cbb + CBOPEN_MAX - 1)) {
  215.         return FALSE;
  216.     }
  217.     if ((((char *)cbp - (char *)cbb)) % sizeof(*cbb) != 0) {
  218.         return FALSE;
  219.     }
  220.  
  221.     return TRUE;
  222. }
  223.